import {AsyncStorage} from 'react-native'

const {Actions} = require('react-native-router-flux');
const STORAGE_KEY = "STORAGE_KEY_sys";
export default {
    namespace: 'sys',
    state: {
        selectArea: null
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        * finishLaunch({payload}, {put, call, select}) {
        },

        * clear({payload}, {call, put}) {
        },
    },
    subscriptions: {
        setup({dispatch}) {
            dispatch({
                type: 'restoreFromStorage'
            })
        }
    },
};
